package pro.cherish.knowledgedemothymeleaf.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;

/**
 * @author 创建人：cherish
 * @version 版本号：V1.0
 * <p>
 * ***************************修订记录************************************
 * 2020-02-07 cherish 创建该类功能。
 * **********************************************************************
 * </p>
 * @ClassName 类名：TemplateConfig
 * @Description 功能说明：
 * <p>
 * TODO
 * </p>
 * **********************************************************************
 * @date 创建日期： 2020-02-07 cherish
 */
@Configuration
public class TemplateConfig {

    /**
     * fixme 注意点
     * templateResolver的prefix与suffix对应你的视图层的文件位置
     * templateResolver的characterEncoding和viewResolver的都要设置成UTF-8中文才不会乱码。
     * templateResolver的cacheable一定要在开发的时候设置成false不然无法看到实时的页面数据
     */

    public final static String CHARACTER_ENCODING = "UTF-8";

    /**
     * thymeleaf模板引擎参数
     */
    public final static String TEMPLATE_PREFIX = "classpath:/templates/";
    public final static String TEMPLATE_SUFFIX = ".html";
    public final static Boolean TEMPLATE_CACHEABLE = false;
    public final static String TEMPLATE_MODE = "HTML5";
    public final static Integer TEMPLATE_ORDER = 1;

    /**
     * @Description: 模板解析器
     * @Param: []
     * @return: org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver
     * @Author: lys
     * @Date: 2020-2-7
     */
    @Bean
    public SpringResourceTemplateResolver templateResolver() {
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setPrefix(TEMPLATE_PREFIX);
        templateResolver.setSuffix(TEMPLATE_SUFFIX);
        templateResolver.setCacheable(TEMPLATE_CACHEABLE);
        templateResolver.setCharacterEncoding(CHARACTER_ENCODING);
        templateResolver.setTemplateMode(TEMPLATE_MODE);
        templateResolver.setOrder(TEMPLATE_ORDER);
        return templateResolver;
    }


    /**
     * @Description: 模板引擎
     * @Param: [templateResolver]
     * @return: org.thymeleaf.spring5.SpringTemplateEngine
     * @Author: lys
     * @Date: 2020-2-7
     */

    @Bean
    public SpringTemplateEngine springTemplateEngine(SpringResourceTemplateResolver templateResolver) {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver);
        return templateEngine;
    }
}
